<template>
  <div id="box">
    <div class="write">填写请假条</div>
    <div class="content">
      <el-select v-model="value" placeholder="请选择">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.label"
        >
        </el-option>
      </el-select>
      <div class="block">
        <el-date-picker
          v-model="value2"
          value-format="yyyy-MM-dd"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
        >
        </el-date-picker>
      </div>
      <div class="reason">
        <textarea placeholder="请输入原因" v-model="content"></textarea>
      </div>

      <div class="btn">
        <el-button type="primary" @click="open()">提交</el-button>
      </div>
      <div class="result">
        <div class="title">请假记录</div>
        <div class="recording">
          <el-table :data="tableData" stripe style="width: 100%">
            <el-table-column prop="date" label="申请时间"> </el-table-column>
            <el-table-column prop="type" label="类型"> </el-table-column>
            <el-table-column prop="start_time" label="开始时间">
            </el-table-column>
            <el-table-column prop="end_time" label="结束时间">
            </el-table-column>
            <el-table-column prop="result" label="审核结果"> </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  created: function () {
    this.getIndex();
  },
  methods: {
    getIndex() {
      this.$api({
        url: "/index/askforleave_data",
        method: "post",
        data: { count: window.localStorage.getItem("username") },
        success: (data) => {
          console.log(data);
          let tmp;
          data.forEach((value) => {
            if (value.allow == 0) {
              tmp = "未审核";
            } else if (value.allow == 1) {
              tmp = "审核未通过";
            } else {
              tmp = "审核通过";
            }
            var listdata = {
              date: value.create_time.substring(0, 10),
              type: value.type,
              start_time: value.start_time,
              end_time: value.end_time,
              result: tmp,
            };
            this.tableData.push(listdata);
            listdata = "";
          });
        },
      });
    },
    open() {
      if (!this.value) {
        alert("请假类型需要填写完整！");
      } else if (!this.value2) {
        alert("请假时间需要填写完整！");
      } else if (!this.content) {
        alert("请假内容需要填写完整！");
      } else {
        let arr = [...this.value2];
        console.log(arr[0]);
        console.log(this.content);
        this.$api({
          url: "/index/qingjia",
          method: "post",
          data: {
            count: window.localStorage.getItem("username"),
            type: this.value,
            start_time: arr[0],
            end_time: arr[1],
            content: this.content,
          },
          success: (data) => {
            alert(data);
            (this.value = ""), (this.value2 = ""), (this.content = "");
            this.tableData = [];
            this.getIndex();
          },
        });
      }
    },
  },
  data() {
    return {
      // 选项
      options: [
        {
          value: "选项1",
          label: "病假",
        },
        {
          value: "选项2",
          label: "事假",
        },
      ],
      value: "",
      content: "",
      // 日期
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      value1: "",
      value2: "",
      rules: {
        value2: [
          {
            type: "string",
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
      },
      tableData: [],
    };
  },
};
</script>

<style lang="scss" scoped>
#box {
  border: 2px solid #ccc;
  .write {
    font-size: 20px;
    text-align: center;
    height: 50px;
    font-weight: bold;
    line-height: 50px;
    border-bottom: 1px solid #ccc;
  }
  .content {
    padding: 0 20px;

    .el-select {
      padding: 10px 0;
    }
    .block {
      padding: 10px 0;
    }
    .reason {
      padding: 10px 0;
      textarea {
        width: 90%;
        height: 50px;
        outline-style: none;
        resize: none;
        display: block;
        border: 1px solid #dcdfe6;
        padding: 10px;
      }
    }
    .btn {
      padding: 10px 0 30px 0;
      padding-right: 120px;
      display: flex;
      justify-content: flex-end;
      border-bottom: 1px solid #ccc;
      margin-bottom: 30px;
    }
    .result {
      padding-bottom: 50px;
      .title {
        font-size: 20px;
        color: #ccc;
        transition: all 0.5s ease;
        text-align: center;
        padding: 8px;
        &::before {
          content: "";
          display: inline-block;
          height: 2px;
          width: 50px;
          background: #ccc;
          position: relative;
          top: -5px;
          transition: all 0.5s ease;
        }
        &::after {
          content: "";
          display: inline-block;
          height: 2px;
          width: 50px;
          background: #ccc;
          position: relative;
          top: -5px;
          transition: all 0.5s ease;
        }
      }
      &:hover .title {
        color: skyblue;
        transition: all 0.5s ease;
      }
      &:hover .title::before {
        width: 200px;
        background: skyblue;
        transition: all 0.5s ease;
      }
      &:hover .title::after {
        width: 200px;
        background: skyblue;
        transition: all 0.5s ease;
      }
    }
  }
}
</style>
